package com.yksj.healthtalk.wallet;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog.OnClickSureBtnListener;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.MD5Utils;
import com.yksj.healthtalk.utils.SystemUtils;


/**
 * 结算
 * @author Administrator
 *
 */
public class SettlementActivity extends BaseFragmentActivity implements OnClickListener{
	
	private String settlementMessage;
	private EditText mMoney;
	private EditText payee;
//	private EditText telephone;
	private EditText account;
	private EditText bankName;
//	private EditText subbranchName;
	private String pwd;
	private String money;
	private String isBDPhoneNum;//手机号
	private String mBankNumber;//银行卡号
	private TextView tv2;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.settlement_layout);
		initWidget();
		initDate();
		
	}

	private void initDate() {
		settlementMessage = getIntent().getStringExtra("settlementMessage");
		money = getIntent().getStringExtra("money");
		isBDPhoneNum=getIntent().getStringExtra("isBDPhoneNum");
		mBankNumber=getIntent().getStringExtra("bank");
		
			
		if (!TextUtils.isEmpty(settlementMessage)) {
			try {
//				   --收款人(单位):PAYEE
//				   --联系电话:TELEPHONE
//				   --银行账号:BANK_ACCOUNT
//				   --开户行:BANK_FULL_NAME
//				   --开户行支行:SUBBRANCH_NAME
				JSONObject object = JSON.parseObject(settlementMessage);
				mMoney.setText(money);
				payee.setText(object.getString("PAYEE"));	
//				telephone.setText(object.getString("TELEPHONE"));
				account.setText(object.getString("BANK_ACCOUNT"));
				bankName.setText(object.getString("BANK_FULL_NAME"));
//				subbranchName.setText(object.getString("SUBBRANCH_NAME"));
//				pwd.setText(object.getString("PAYMENT_PASS"));
				if(!TextUtils.isEmpty(mBankNumber))
					account.setText(mBankNumber);
				
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		initTitle();
		mMoney = (EditText) findViewById(R.id.withdrawal_money);
		payee = (EditText) findViewById(R.id.payee);
//		telephone = (EditText) findViewById(R.id.telephone);
		account = (EditText) findViewById(R.id.bank_account);
		bankName = (EditText) findViewById(R.id.bank_full_name);
//		subbranchName = (EditText) findViewById(R.id.subbranch_name);
//		pwd = (EditText) findViewById(R.id.pwd);
		
		titleTextV.setText("结算");
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setOnClickListener(this);
		titleLeftBtn.setOnClickListener(this);
		//银行卡自动获取
//		CustomerInfoEntity entity=SmartFoxClient.getLoginUserInfo();
//		String cast=entity.getTransferCode();
//		if(!TextUtils.isEmpty(cast)){
//			account.setText(cast);
//		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			SystemUtils.hideSoftBord(this, account);
			finish();
			break;
			
		case R.id.title_right2:
//			payee = (EditText) findViewById(R.id.payee);
//			telephone = (EditText) findViewById(R.id.telephone);
//			account = (EditText) findViewById(R.id.bank_account);
//			bankName = (EditText) findViewById(R.id.bank_full_name);
//			subbranchName = (EditText) findViewById(R.id.subbranch_name);
//			pwd = (EditText) findViewById(R.id.pwd);
			if (mMoney.getText().toString().trim().equals("")) {
				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),"结算金额不能为空");
				return;
			}else if(payee.getText().toString().trim().equals("")){
				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "收款人不能为空");
				return;
//			}else if(telephone.getText().toString().trim().equals("")){
//				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "联系电话不能为空");
//				return;
			}else if(account.getText().toString().trim().equals("")){
				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "银行账号不能为空");
				return;
			}else if(bankName.getText().toString().trim().equals("")){
				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "开户行不能为空");
				return;
//			} else if(subbranchName.getText().toString().trim().equals("")){
//				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "开户行支行不能为空");
//				return;
//			}else if(pwd.getText().toString().trim().equals("")){
//				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "支付密码不能为空");
//				return;
			}
			
//			if (!SystemUtils.checkPhone(telephone.getText().toString().trim())) {
//				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "电话号码格式不正确");
//				return;
//			}
			//对话框
			dialog();

			break;
		default:
			break;
		}
	}
    
	private void dialog() {
		final Dialog dialog = new Dialog(this,R.style.translucent_dialog);
		
		dialog.setCancelable(false);
		dialog.setContentView(R.layout.my_dialog_money);
		TextView titleTxtV = (TextView)dialog.findViewById(R.id.dialog_title);
		Button button1 = (Button)dialog.findViewById(R.id.dialog_cancel);
		Button button2 = (Button)dialog.findViewById(R.id.dialog_ok);
		TextView tv=(TextView) dialog.findViewById(R.id.pass);
		 tv2=(TextView) dialog.findViewById(R.id.etname);

		tv.setOnClickListener(new OnClickListener() {//忘记密码
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),PwdForgetActivity.class);
				intent.putExtra("isBDPhoneNum", isBDPhoneNum);
				startActivity(intent);
			}
		});
		button1.setOnClickListener(new View.OnClickListener() {//取消
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		button2.setOnClickListener(new OnClickListener() {//确定
			@Override
			public void onClick(View v) {
//				客户ID：CUSTOMERID
//				结算金额：WITHDRAWAL_MONEY
//				收款人(单位):PAYEE
//				联系电话:TELEPHONE
//				银行账号:BANK_ACCOUNT
//				开户行:BANK_FULL_NAME
//				开户行支行:SUBBRANCH_NAME
//				支付密码:PAYMENT_PASS
				pwd=tv2.getText().toString().trim();
				RequestParams params = new RequestParams();
				params.put("CUSTOMERID", SmartFoxClient.getLoginUserId());
				params.put("WITHDRAWAL_MONEY", mMoney.getText().toString().trim());
				params.put("PAYEE",payee.getText().toString().trim());
//				params.put("TELEPHONE", telephone.getText().toString().trim());
				params.put("BANK_ACCOUNT", account.getText().toString().trim());
				params.put("BANK_FULL_NAME",bankName.getText().toString().trim());
//				params.put("SUBBRANCH_NAME",subbranchName.getText().toString().trim());
				params.put("PAYMENT_PASS",MD5Utils.getMD5(pwd));
				
				HttpRestClient.doHttpSettlement(params, 
						new JsonsfHttpResponseHandler(SettlementActivity.this){
					@Override
					public void onSuccess(int statusCode, JSONObject response) {
						if (response.containsKey("INFO")) {
							final String info=response.getString("INFO");
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),info,new OnClickSureBtnListener() {
								@Override
								public void onClickSureHander() {
									if("结算成功，请等待收款..".equals(info)){
										finish();
										}
								}
							});
						}
						super.onSuccess(statusCode, response);
					}
				});
			}
		});
		titleTxtV.setText("钱包支付密码");
		button1.setText("取消");
		button2.setText("确定");
		dialog.show();
	}


}
